import {
  Controller,
  Post,
  Body,
  Request,
  Get,
  Delete,
  Put,
} from '@nestjs/common';
import {
  ApiBody,
  ApiOperation,
  ApiQuery,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { UserService } from './user.service';
import { Public } from '../user/auth/custom.decorator';
import { ResOp } from '../shared/interceptor/response.interceptor';
import {
  CreateUserDto,
  LoginUserDto,
  DeleteUserDto,
  SendCaptchaDto,
  UserInfoReponseDto,
  LoginUserReponseDto,
  UpdateUserDto,
  ResetPasswordDto,
} from './dto';

@Controller('user')
@ApiTags('用户模块')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Public()
  @Post('register')
  @ApiOperation({ summary: '注册' })
  @ApiResponse({ type: UserInfoReponseDto })
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }

  @Public()
  @Post('login')
  @ApiOperation({
    summary: '登录',
    description: '密码登录或验证码登录二选一(当前只支持密码登录)',
  })
  @ApiResponse({ type: LoginUserReponseDto })
  login(@Body() loginUserDto: LoginUserDto) {
    return this.userService.login(loginUserDto);
  }

  @Public()
  @Post('logout')
  @ApiOperation({
    summary: '退出登录',
    description: '退出登录，暂无具体实现',
  })
  logout() {
    return this.userService.logout();
  }

  @Get('info')
  @ApiOperation({
    summary: '获取用户信息',
    description: '传入id获取指定id用户信息，否则返回当前用户信息',
  })
  @ApiQuery({ name: 'id', description: '用户id', required: false })
  @ApiResponse({ type: UserInfoReponseDto })
  getUserInfo(@Request() request) {
    const userId: number = request.query.id ?? request.user.userId;
    return this.userService.getUserInfoById(userId);
  }

  @Put('update')
  @ApiOperation({
    summary: '更新用户信息',
    description: '传入id更新指定id用户信息，否则更新当前用户信息',
  })
  @ApiBody({
    type: UpdateUserDto,
    description: '用户id和用户信息',
  })
  @ApiResponse({ type: UserInfoReponseDto })
  updateUserInfo(@Request() request, @Body() body: UpdateUserDto) {
    const { id, ...args } = body;
    const userId: number = id ?? request.user.userId;
    return this.userService.updateUserInfoById(userId, args);
  }

  @Put('reset')
  @Public()
  @ApiOperation({
    summary: '找回密码(重置)',
  })
  @ApiBody({
    type: ResetPasswordDto,
  })
  @ApiResponse({ type: ResOp })
  resetPassword(@Body() body) {
    console.log(body);
    return this.userService.resetPassword(body);
  }

  @Delete('delete')
  @ApiOperation({ summary: '删除用户', description: '删除用户(支持批量)' })
  @ApiBody({
    type: DeleteUserDto,
    description: '用户id数组',
  })
  @ApiResponse({ type: ResOp })
  deleteUser(@Body() body: DeleteUserDto) {
    return this.userService.deleteUserById(body.idList);
  }

  @Public()
  @Post('captcha')
  @ApiOperation({
    summary: '发送验证码',
    description: '支持登录、注册、找回密码等验证操作',
  })
  @ApiBody({ type: SendCaptchaDto })
  @ApiResponse({ type: ResOp })
  sendCaptcha(@Request() request, @Body() body: SendCaptchaDto) {
    return this.userService.sendCaptcha(
      body.email,
      body.operationType,
      request.connection.remoteAddress,
    );
  }
}
